package ssau.knyazev.core.image;

import ssau.knyazev.core.Complex;

public class ComplexImageModel {

	protected Complex[][] values = null; 

	public ComplexImageModel(int height, int width){
		values = new Complex[height][width];
	}
	
	public ComplexImageModel(Complex[][] src){
		values = src;
	}
	
	public Complex[][] getValues(){
		return values;
	}
	
	public void setValues(Complex[][] values){
		this.values = values;
	}
		
	public ImageModel getImageModel(boolean contrast){
		if (contrast){
			return getImageModel();
		} else {
			float[][] arr = new float[values.length][values[0].length];
			for (int i = 0; i < arr.length; i++){
				for (int j = 0; j < arr[0].length; j++){
					arr[i][j] = values[i][j].abs();
					arr[i][j] = (arr[i][j] > 255) ? 255 : arr[i][j];
					arr[i][j] = (arr[i][j] < 0) ? 0 : arr[i][j];
				}
			}
			return new ImageModel(arr);
		}
	}
	
	public ImageModel getImageModel(){
		float[][] arr = new float[values.length][values[0].length];
		for (int i = 0; i < arr.length; i++){
			for (int j = 0; j < arr[0].length; j++){
				arr[i][j] = (values[i][j].abs() > 255) ? 1000 : values[i][j].abs();
			}
		}
		
		double minX = 255;
		double maxX = 0;
		for (int i = 0; i < arr.length; i++){
			for (int j = 0; j < arr[0].length; j++){
				if (arr[i][j] > maxX){
					maxX = arr[i][j];
				}
				if (arr[i][j] < minX){
					minX = arr[i][j];
				}
			}
		}
		double a = 255/(maxX - minX);
		double b = a*minX;
		for (int i = 0; i < arr.length; i++){
			for (int j = 0; j < arr[0].length; j++){
				arr[i][j] = (float)(a*arr[i][j] - b);
				arr[i][j] = arr[i][j] > 255 ? 255 : arr[i][j];
			}
		}
		
		return new ImageModel(arr);
	}
	
	public String toString(){
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < values.length; i++){
			for (int j = 0; j < values[0].length; j++){
				sb.append(values[i][j] + "  ");
			}
			sb.append("\n");
		}
		return sb.toString();
	}
	
	public ComplexImageModel clone(){
		Complex[][] arr = new Complex[values.length][values[0].length];
		for (int i = 0; i < values.length; i++){
			for (int j = 0; j < values[0].length; j++){
				arr[i][j] = values[i][j].clone();
			}
		}
		return new ComplexImageModel(arr);
	}
	
}
